package org.yxing.palpalgps.activity;

import org.json.JSONException;
import org.json.JSONObject;
import org.yxing.palpalgps.BaseActivity;
import org.yxing.palpalgps.R;
import org.yxing.palpalgps.domain.AsyncImageLoader;
import org.yxing.palpalgps.domain.UserInfo;
import org.yxing.palpalgps.http.AsyncRequest;
import org.yxing.palpalgps.http.AsyncRequest.FetchListener;
import org.yxing.palpalgps.utils.ConfigHelper;
import org.yxing.palpalgps.utils.Helper;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;

public class ProfileActivity extends BaseActivity implements OnClickListener {
    
    UserInfo user;
    
    private ImageView portraitView;
    private TextView nameView;
    
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.profile);
		setTitle(getString(R.string.profile));
		
		user = ConfigHelper.nowUser;
		portraitView = (ImageView) findViewById(R.id.portriaitView);
		nameView = (TextView) findViewById(R.id.nameView);
		
		findViewById(R.id.profileEdit).setOnClickListener(this);
		findViewById(R.id.photoList).setOnClickListener(this);
		findViewById(R.id.blacklistLayout).setOnClickListener(this);
		findViewById(R.id.friendAlertsLayout).setOnClickListener(this);
		findViewById(R.id.seekLayout).setOnClickListener(this);
		
		getUser();
	}
	
	public void onResume() {
	    super.onResume();
	    showSimpleProfile();
	}
	
	   
    /**
     * 获取用户个人信息
     */
    private void getUser() {
        AsyncRequest ar = new AsyncRequest(this);
        ar.fetch("profile/my", new FetchListener() {
            
            public void onError(int code, String msg) {
                displayToast(msg);
            }
            
            public void onComplete(JSONObject response) {
                try {
                    JSONObject result = response.getJSONObject("result");
                    user.setName(result.getString(UserInfo.KEY_NAME));
                    user.setGender(result.getString(UserInfo.KEY_GENDER));
                    user.setFace(result.getString(UserInfo.KEY_FACE));
                    user.setBirthday(result.getString(UserInfo.KEY_BIRTHDAY));
                    user.setRelation(result.getString(UserInfo.KEY_RELATION));
                    user.setSeekGender(result.getString(UserInfo.KEY_SEEK_GENDER));
                    user.setSeekAge(result.getString(UserInfo.KEY_SEEK_AGE));
                    user.setSeekAim(result.getString(UserInfo.KEY_SEEK_AIM));
                    user.setSeekDetail(result.getString(UserInfo.KEY_SEEK_DETAIL));
                    
                    showSimpleProfile();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }
	
    private void showSimpleProfile() {
        //show portrait
        AsyncImageLoader ail = new AsyncImageLoader();
        String pictureUrl = Helper.getSmallPictureUrl(user.getFace());
        ail.loadDrawable(pictureUrl, portraitView);
        
        //show name
        nameView.setText(user.getName());
    }
	
	public boolean onKeyUp(int keyCode, KeyEvent event) {
	    
	    if(keyCode == KeyEvent.KEYCODE_BACK) {
	        return false;
	    }
	    
	    return super.onKeyUp(keyCode, event);
	}
	
	public void onClick(View v) {
	    switch (v.getId()) {
	    case R.id.profileEdit:
	        startActivity(ProfileEditActivity.class);
	        break;
	    case R.id.photoList:
	        startActivity(PhotoGridActivity.class);
	        break;
	    case R.id.blacklistLayout:
	        startActivity(BlackListActivity.class);
	        break;
	    case R.id.friendAlertsLayout:
	        startActivity(FriendAlertActivity.class);
	        break;
	    case R.id.seekLayout:
	        startActivity(SeekSettingActivity.class);
	        break;
	    }
	}
	
	private void startActivity(Class<? extends Activity> c) {
	    Intent intent = new Intent(this, c);
	    startActivity(intent);
	}
}